//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
//		 M A S T E R  E F F E C T  1.0		     //
//	   effect.txt shader file for GTA San Andreas        //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
// 		   made by : Marty McFly                     //
// 		Original Codes & Shaders by: 		     //		
//      Matso, Ceejay.dk, SSontech, icelaglace, AAA	     //
//      Reinhard, HeliosDoubleSix, PetkaGtA, DKT70           //
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//

//How to use:
//If you want to enable an effect, remove // before #define [Effect], if you want to disable
//it again, put two slashes before the #define [Effect].
//Enabled effect:            #define [Effect]
//Disabled effect:           //#define [Effect]
//Information is given for almost all values, play with it!
//If you have questions, contact me on GTAForums (my Nick: Marty McFly, language please English or German) 
//or ask for help in the ENB Screenshots Thread (http://www.gtaforums.com/index.php?showtopic=488840)
//Feel free to use this effect.txt in your ENB packages but please give credits; stealing is no fun
//BY DEFAULT EVERY EFFECT IS DISABLED!
//////////////////////////////////-----------------------------------------------------------------]
//////////////////////////////////-----------------------------------------------------------------]
//////////////////////////////////-----------------------------------------------------------------]


//Image Noise, adds a slight (or not so slight, depends on settings) grain to the screen like
//when a TV has no signal but far not so intensive, gives a movie-like feeling
//Original code by Matso

//#define NOISE

#ifdef NOISE
float4	Timer;								//leave as it is, needed for grain frequency
#define SEED			Timer.w					//same
#define fGrainFreq 500.0						// image grain frequency, for some reason high values cause shadow flickering
#define fGrainScale 0.075						// grain effect scale
#endif

//Image Dithering
//Applies dithering to simulate more colors than your monitor can display. 
//This lessens banding artifacts (mostly caused by Vignette and enbpalette which got edited many times)
//Note: Screenshot filesize gets slightly bigger because now there are less areas with the same color
//Ported from SweetFX by Ceejay.dk
//////////////////////////////////-----------------------------------------------------------------]

#define USE_DITHER
float screen_size = 1600; 				// Horizontal screen resolution

//Vibrance
//Intelligently saturates (or desaturates if you use negative values) the pixels depending on their original saturation.
//Ported from SweetFX by Ceejay.dk
//////////////////////////////////-----------------------------------------------------------------]

#define VIBRANCEPASS
#ifdef VIBRANCEPASS
float Vibrance = 0.4; 					//[-1.0 to 1.0] 
#endif

// Chromatic Abberation & Lens Distortion code by SSontech, implementing in ENB by IceLaGlace
// convert and all stuff to have it here: me
//////////////////////////////////-----------------------------------------------------------------]

//#define CHROMATICABBERATION

#ifdef CHROMATICABBERATION
float ChromaticAmount = 0.015; 				// Amount of chromatic aberration on the edges
float LensSize = 0.6;   				// 0.5 = Original image size; 1.0 = Zoomed
float LensDistortion = 0.1;   				// Can be negative to stretch the image
float LensDistortionCubic = 0.1;			// similiar to above, better results if same like LensDistortion
#endif


//FXAA v3 by Timothy Lottes
//Cannot be disabled because shader would get incredibly entangled, my brain doesn't stand this

  #define FXAA_LINEAR 0
  #define FXAA_QUALITY__EDGE_THRESHOLD (1.0/16.0)
  #define FXAA_QUALITY__EDGE_THRESHOLD_MIN (1.0/16.0)
  #define FXAA_QUALITY__SUBPIX_CAP (3.0/4.0)
  #define FXAA_QUALITY__SUBPIX_TRIM (1.0/4.0)
  #define FXAA_QUALITY__SUBPIX_TRIM_SCALE  (1.0/(1.0 - FXAA_QUALITY__SUBPIX_TRIM))
  #define FXAA_SEARCH_STEPS     8
  #define FXAA_SEARCH_THRESHOLD (1.0/4.0)

// DKT70 HDR
//////////////////////////////////-----------------------------------------------------------------]

//#define HDR

float Defog=0.000; 					// Strength of FogColor, higher = more.
float4 FogColor={0.0, 0.0, 0.0, 0.0}; 			// Lens-style color filters for Blue, Red, Yellow, White.
float Exposure=1.47; 					// Contrast settings, higher = brighter, but also more white.
float Gamma=0.186; 					// Gamma settings for darker or lighter shadows and dark areas, higher = darker.
float BlueShift=0.25; 					// Shifts entire color spectrum towards blue, good for images too yellow, but this is global.

// DKT70 Sharpen effect
//////////////////////////////////-----------------------------------------------------------------]

float sharps = 0.0; 					//sharpen strength; always offset value x 2
float offsetv = 0.0;					//sharpen offset 
float sxres = 1920; 					// Horizontal Resolution setting x 2
float syres = 1080; 					// Vertical Resolution setting x 2
float aspect = 1.77777777778; 				// horizontal/vertical resolution


// Anamorphic flare, based on Petka's bloom and CC
//////////////////////////////////-----------------------------------------------------------------]

//#define ANAMORPHICFLARE

#ifdef ANAMORPHICFLARE
#define FLAREMODE_1					//Flaremode 1 = new flare by me; Flaremode 2 = colormod-type flare
//#define FLAREMODE_2

//the flares work differently so there are different values for the Threshold and power needed.

static const float thresh1 = 1.60; 			//Flaremode 1 Threshold
static const float thresh2 = 40.0;			//Flaremode 2 Threshold

float fFlareIntensity1 = 9.00;				//Flaremode 1 Intensity
float fFlareIntensity2 = 0.20;				//Flaremode 2 Intensity
	  		
float4 paramsS = {1.0,1.0,1.0,1.0}; 			//Colorcorrection for dark areas
float4 paramsM = {1.0,1.0,1.0,1.0}; 			//Colorcorrection for medium areas
float4 paramsH = {1.0,1.0,1.0,1.0}; 			//Colorcorrection for bright areas

const float fBlurSize = 1.0/16.0;			//How big the flare appears, don't go too high or it will look ugly
const float fBlurDownsampling = 32;			//Something IDK, remained from Petka work, only change in powers of 2 like 16, 32, 64 etc.
#endif

// Flare tintcolor, if you want it colored

#define ANAMORPHICTINTCOLOR
	#define ANAMORPHICREDAMOUNT  		1.0 	//power of red godray color
	#define ANAMORPHICGREENAMOUNT 		1.0 	//power of green godray color
	#define ANAMORPHICBLUEAMOUNT 		1.0 	//power of blue godray color

// Light Scattering ("Godrays") implementation for SA by PetkaGtA, coloring by me
//////////////////////////////////-----------------------------------------------------------------]

//#define LIGHTSCATTERING
        
	//#define TINTCOLOR   				//Shifts godray color
	#define COLORING				//Colorizes godrays, original color gets lost
	#define REDAMOUNT  		1.2 		//power of red godray color
	#define GREENAMOUNT 		0.8 		//power of green godray color
	#define BLUEAMOUNT 		2.0 		//power of blue godray color
   
#ifdef LIGHTSCATTERING
float2 ScreenLightPos = {0.5,0.5};			//where the Godrays appear, 0.5, 0.5 means screen center
float Density = 1.50;					//power of the rays
static int NUM_SAMPLES = 128;				//how many samples
float Weight = 0.3;
float Decay = 1.0;
float SStresh = 0.93;					//pixel threshhold, above this level pixels get sampled, let it stay like this!
float SunExposure = 1.0;				//Sun gets brighter with this value
#endif


// Ministry of Games 0.1 filmic color correction by openzdl aka AAA (AgainstAllAuthority) convert by me
//////////////////////////////////-----------------------------------------------------------------]

#define FILMICPASS

#ifdef FILMICPASS
	float Strenght 		= 0.25;			//  from 0.0 to 1.0
	float BaseGamma 	= 1.6;			//  from 0.1.0 to 2.5
	float Fade		= 0.2;			//  from 0.0 to 0.5
	float Contrast		= 1.0;			//  from 0.6 to 1.0
	float Saturation	= 0.15;			//  from 0.25 to -0.25
	float Bleach		= 0.005;		//  from 0.0 to 1.0
	float RedCurve		= 6.0;			//  from 1.0 to 10.0
	float GreenCurve	= 6.0;			//  from 1.0 to 10.0
	float BlueCurve		= 6.0;			//  from 1.0 to 10.0
	float BaseCurve		= 1.5;			//  from 1.0 to 10.0
	float EffectGammaR	= 1.0;			//  from 0.1 to 2.5
	float EffectGammaG	= 1.0;			//  from 0.1 to 2.5
	float EffectGammaB	= 1.0;			//  from 0.1 to 2.5
	float EffectGamma	= 0.75;			//  from 0.1 to 2.5
	float Linearization	= 1.3;			//  from 1.0 to 2.5
#endif

//Reinhard tonemap. you have to get used to it, very strange look, 
//you have to build up your setting on it
//////////////////////////////////-----------------------------------------------------------------]

#define REINHARD_ICE21
	#define REINHARD_SAT	 1.4 			//Saturation only for Reinhard tonemap
	#define TONEMAPEXP 	 3.1 			//Exposure only for Reinhard tonemap
	#define LUM_FACTOR 	float4(0.299, 0.587, 0.114, 0.0) //DON NOT CHANGE IT

//Depth of Field values, shader by PetkaGtA, thank you :]
//////////////////////////////////-----------------------------------------------------------------]

//#define DOF

#ifdef DOF

//constants, don't change

float ScreenScaleY;
float ScreenSize;
float PI = 3.14159265;
float2 texel = {0.0009765625,0.00130208333333333333333333333333};

//your screen resolution

float2 screenRes = {1600,900};

//DOF params

float focalDepth = 20.5;
float focalLength = 10.5;
float fstop = 20.5;
bool manualdof = false; 				//manual dof calculation
float ndofstart = 1.0; 					//near dof blur start
float ndofdist = 2.0; 					//near dof blur falloff distance
float fdofstart = 1.0; 					//far dof blur start
float fdofdist = 3.0; 					//far dof blur falloff distance
bool autofocus = true;
float2 focus = float2(0.5,0.5);
float CoC = 0.300;					//Circle of confusion http://en.wikipedia.org/wiki/Circle_of_confusion
bool noise = false; 					//DOF noise, adds grain
float namount = 0.0000; 				//grain amount
float DOFdownsample = 9;
float maxblur = 3.0; //Power of DOF blur
static const int samples = 26; 				//more samples =better quality but also less fps
static const int rings = 1; 				//ring count, DO NOT GO BEYOND 2 OR GAME CRASH!!!
float threshold = 0.9;					//threshold for gain
float gain = 1.0;					//brightens blurred areas to make bokeh effect visible
float bias = 0.0;
float fringe = 1.0;
float znear = 2.0; 					//camera clipping start (blur start)
float zfar = 2000.0; 					//camera clipping end (distance beyond which blur doesn't get bigger anymore)
bool pentagon = false; 					//use pentagon as bokeh shape? Bugged!
float feather = 0.0; 					//pentagon shape feather

//vignetting, darkens screen borders
bool vignetting = true;
float vignout = 25.31; 					//vignetting outer border
float vignin = 5.30; 					//vignetting inner border
float MartyMcFly = 25.30; 				//f-stops till vignette fades
#endif



// simple gamma Correction, lower = darker
//////////////////////////////////-----------------------------------------------------------------]

#define GAMMACORRECTION
	#define GammaAmount       3.0 			//Gamma intensity

// the well-known filmiccurve (important: darkens screen really hard by default, I've added
// FilmicExposure which again turns up the brightness to a good amount. Changing the ABCDEFW values
// may require changing the FilmicExposure level
//////////////////////////////////-----------------------------------------------------------------]

//#define FILMICCURVE
	#ifdef FILMICCURVE
		float	FilmicExposure = 7;

//configuring the following stuff is very difficult, let it stay like this
//explanation is senseless because only the most advanced coders really understand 
//what each value does (not even me), maybe only the author. Set one to 10 and look what happens
//is the best way to get its effect

		float	ShoulderStrength = 0.50;
		float	LinearStrength = 0.50;
		float	LinearAngle = 0.30;
		float	ToeStrength = 2.50;
		float	ToeNumerator = 0.02;
		float	ToeDenominator = 1.50;
		float	WFilmWeight = 20.0; // 20

		float3 Uncharted2Tonemap(float3 x)
		{
			return ((x*(ShoulderStrength*x+LinearAngle*LinearStrength)+ToeStrength*ToeNumerator)/(x*(ShoulderStrength*x+LinearStrength)+ToeStrength*ToeDenominator))-ToeNumerator/ToeDenominator;
		}
	#endif

// HeliosDoubleSix (HD6) Vignette code
//////////////////////////////////-----------------------------------------------------------------]

//#define HD6_VIGNETTE
	//#define LEFTANDRIGHT 				//only enable one of the three options here!
	#define TOPANDBOTTOM 				//only enable one of the three options here!
	//#define CORNERDARKEN 				//only enable one of the three options here!
	#ifdef HD6_VIGNETTE
	float   SquareTop = 0.58;              		// TOP of Screen.
	float   SquareBottom = 0.58;   			// BOTTOM of Screen.
	float   CircularPower = 0.1;    		// CIRCULAR Vignette Amount.
	float   ColorDistortion = -0.666;  		// COLOR Distortion Strength.
	float   ContrastSharpen = 11.666;       	// Contrast & Sharpness Increase.
	float   VignetteBorder = 5.5;            	// High value means black bar, low means smooth gradient
	#endif

//#define BORISVIGNETTE					//Le standard vignette

#ifdef BORISVIGNETTE
float	EVignetteAmount=2.9;				//Darkening/coloring power
float	EVignetteCurve=1.5;				//Vignette Curve (helpful huh? :D)
float	EVignetteRadius=0.8; 				//Radius beyond which the screen starts to get darkened/colored
	//#define VIGNCOLORING				//optional colorable vignette, ever wanted to make a red vignette or such? here you go

	#define VIGNREDAMOUNT		0.0		//self-explaining I think
	#define VIGNGREENAMOUNT		5.0		//self-explaining I think
	#define VIGNBLUEAMOUNT		0.0		//self-explaining I think
#endif

// Cross Processing by AAA, tries to recreate old TV look
//////////////////////////////////-----------------------------------------------------------------]

//#define CROSSPROCESS
	#ifdef CROSSPROCESS
		float CrossContrast =1.0;		//Image contrast
		float CrossSaturation2 = 1.0;		//Image Saturation
		float CrossBrightness = 0.0;		//Image Brightness
		float CrossAmount = 2.0;		//amount of color changing
		float2 crossMatrix [3] =
		 {
			float2 (1.03, 0.04),
			float2 (1.09, 0.01),
			float2 (0.78, 0.13),
		 };
	#endif

// Cross Process by icelaglace, same like above but not as good 
//////////////////////////////////-----------------------------------------------------------------]

#define CROSSICE
	#ifdef CROSSICE
	float fRatio = 0.55;				//power of the color distortion
	float moodR = 1.0;				//preconfigured by ice
	float moodG = 1.1;				//preconfigured by ice
	float moodB = 0.5;				//preconfigured by ice
	#endif

//main color correction by me and openzdl aka AAA (AgainstAllAuthority)
//Remained in the shader for a long time, I don't want to remove it
//has no visible effect though
//////////////////////////////////-----------------------------------------------------------------]
//#define COLORCORRMAIN

// HSV-based Color Correction, looks much more difficult as it is :)	
//////////////////////////////////-----------------------------------------------------------------]

#define HSVULTIMATE
#ifdef HSVULTIMATE
	#define ENABLE_HSV_CONVERSION  			// Enables HSV Conversion. Required for color controls below.
	#define ENABLE_SATURATION_ADJUSTMENT		// Enables color saturation control for the entire scene.
	#define ENABLE_INTENSITY_ADJUSTMENT  		// Enables brightness control for the entire scene.
	#define ENABLE_HUE_ADJUSTMENT    		// Enables hue control for the entire scene.
	#define ENABLE_COLOR_EQUALIZER    		// Enables the Color Equalizer system for advanced tweaking.

	//#define ENABLE_POW_CURVE    			// Enables power curve controls.

	// Color Saturation Variables
	float	EColorSaturationMod=0.0;  		// Adds to the overall color saturation. Range: -1 to 1. Default: 0.0
	float	EColorSaturationMult=1.0;  		// Multiplies the color saturation. Range: 0.0 (greyscale) to ???. Default: 1.0
	float	EColorSaturationPow=1.0;  		// Curves the color saturation. Range: 0.0 to ???. Default: 1.0

	// Color Intensity Variables
	float   EColorIntensityMod=0.0;  		// Adds to the overall brightness. Range: -1 to 1. Default: 0.0
	float   EColorIntensityMult=1.0;  		// Multiplies the color intensity. Range: 0.0 (black) to ???. Default: 1.0
	float   EColorIntensityPow=1.0;  			// Curves the color intensity. Range: 0.0 to ???. Default: 1.0

	// Color Hue Variables
	float   EColorHueMod=0.0;    			// Adds to the overall color hue (color shifting). Range: -1 to 1. Default: 0.0
	float   EColorHueMult=1.0;    			// Multiplies the color hue (color shifting). Range: 0.0 to ???. Default: 1.0
	float	EColorHuePow=1.0;    			// Curves the color hue. Range: 0.0 to ???. Default: 1.0.

	// Color Equalizer Variables
	// Adjust modifier to specific saturations. Range: -??? to ???. Default: 0.0
	float	SaturationModRed = 0.0;
	float	SaturationModOrange = 0.0;
	float	SaturationModYellow = 0.0;
	float	SaturationModGreen = 0.0;
	float	SaturationModCyan = 0.0;
	float	SaturationModBlue = 0.0;
	float	SaturationModMagenta = 0.0;

	// Adjust multiplier to specific saturations. Range: 0.0 to ???. Default: 0.0
	float	SaturationMultRed = 0.1;
	float	SaturationMultOrange = -0.2;
	float	SaturationMultYellow = 0.0;
	float	SaturationMultGreen = 0.2;
	float	SaturationMultCyan = -0.1;
	float	SaturationMultBlue = 0.0;
	float	SaturationMultMagenta = 0.1;

	// Adjust curve of specific saturations. Range: -1.0 to ???. Default: 0.0
	float	SaturationPowRed = 0.0;
	float	SaturationPowOrange = 0.0;
	float	SaturationPowYellow = 0.0;
	float	SaturationPowGreen = 0.0;
	float	SaturationPowCyan = 0.0;
	float	SaturationPowBlue = 0.0;
	float	SaturationPowMagenta = 0.0;
#endif

//////////////////////////////////-----------------------------------------------------------------]
//////////////////////////////////////////////
// DO NOT MODIFY ANYTHING BELOW THIS LINE,  //
// UNLESS YOU KNOW HLSL SHADER PROGRAMMING. //
//////////////////////////////////////////////
//////////////////////////////////-----------------------------------------------------------------]


#ifdef HSVULTIMATE
// List of floating point values for hues, used in equalizer.
float	HueRed = 0.0;
float	HueOrange = 0.08333333;
float	HueYellow = 0.16666667;
float	HueGreen = 0.33333333;
float	HueCyan = 0.5;
float	HueBlue = 0.66666667;
float	HueMagenta = 0.83333333;
float	HueRed2 = 1.0;

float	HueSkin = 0.07;
float	HueSky = 0.58;


float ColorEqualizerMult(in float H)
{
float SMult = 1.0;
SMult += SaturationMultRed * ( 1.0 - min( 1.0, abs( ( HueRed - H ) / ( HueRed - HueOrange ) ) ) );
SMult += SaturationMultOrange * ( 1.0 - min( 1.0, abs( ( HueOrange - H ) / ( HueOrange - HueYellow ) ) ) );
SMult += SaturationMultYellow * ( 1.0 - min( 1.0, abs( ( HueYellow - H ) / ( HueYellow - HueGreen ) ) ) );
SMult += SaturationMultGreen * ( 1.0 - min( 1.0, abs( ( HueGreen - H ) / ( HueGreen - HueYellow ) ) ) );
SMult += SaturationMultCyan * ( 1.0 - min( 1.0, abs( ( HueCyan - H ) / ( HueCyan - HueGreen ) ) ) );
SMult += SaturationMultBlue * ( 1.0 - min( 1.0, abs( ( HueBlue - H ) / ( HueBlue - HueCyan ) ) ) );
SMult += SaturationMultMagenta * ( 1.0 - min( 1.0, abs( ( HueMagenta - H ) / ( HueMagenta - HueBlue ) ) ) );
SMult += SaturationMultRed * ( 1.0 - min( 1.0, abs( ( HueRed2 - H ) / ( HueRed2 - HueMagenta ) ) ) );
return SMult;
}

float ColorEqualizerMod(in float H)	
{
float SMod = 0.0;
SMod += SaturationModRed * ( 1.0 - min( 1.0, abs( ( HueRed - H ) / ( HueRed - HueOrange ) ) ) );
SMod += SaturationModOrange * ( 1.0 - min( 1.0, abs( ( HueOrange - H ) / ( HueOrange - HueYellow ) ) ) );
SMod += SaturationModYellow * ( 1.0 - min( 1.0, abs( ( HueYellow - H ) / ( HueYellow - HueGreen ) ) ) );
SMod += SaturationModGreen * ( 1.0 - min( 1.0, abs( ( HueGreen - H ) / ( HueGreen - HueYellow ) ) ) );
SMod += SaturationModCyan * ( 1.0 - min( 1.0, abs( ( HueCyan - H ) / ( HueCyan - HueGreen ) ) ) );
SMod += SaturationModBlue * ( 1.0 - min( 1.0, abs( ( HueBlue - H ) / ( HueBlue - HueCyan ) ) ) );
SMod += SaturationModMagenta * ( 1.0 - min( 1.0, abs( ( HueMagenta - H ) / ( HueMagenta - HueBlue ) ) ) );
SMod += SaturationModRed * ( 1.0 - min( 1.0, abs( ( HueRed2 - H ) / ( HueRed2 - HueMagenta ) ) ) );
return SMod;
}

float ColorEqualizerPow(in float H)	
{
float SPow = 1.0;
SPow += SaturationPowRed * ( 1.0 - min( 1.0, abs( ( HueRed - H ) / ( HueRed - HueOrange ) ) ) );
SPow += SaturationPowOrange * ( 1.0 - min( 1.0, abs( ( HueOrange - H ) / ( HueOrange - HueYellow ) ) ) );
SPow += SaturationPowYellow * ( 1.0 - min( 1.0, abs( ( HueYellow - H ) / ( HueYellow - HueGreen ) ) ) );
SPow += SaturationPowGreen * ( 1.0 - min( 1.0, abs( ( HueGreen - H ) / ( HueGreen - HueYellow ) ) ) );
SPow += SaturationPowCyan * ( 1.0 - min( 1.0, abs( ( HueCyan - H ) / ( HueCyan - HueGreen ) ) ) );
SPow += SaturationPowBlue * ( 1.0 - min( 1.0, abs( ( HueBlue - H ) / ( HueBlue - HueCyan ) ) ) );
SPow += SaturationPowMagenta * ( 1.0 - min( 1.0, abs( ( HueMagenta - H ) / ( HueMagenta - HueBlue ) ) ) );
SPow += SaturationPowRed * ( 1.0 - min( 1.0, abs( ( HueRed2 - H ) / ( HueRed2 - HueMagenta ) ) ) );
return SPow;
}

float3 HUEtoRGB(in float H)
{
   float R = abs(H * 6.0 - 3.0) - 1.0;
   float G = 2.0 - abs(H * 6.0 - 2.0);
   float B = 2.0 - abs(H * 6.0 - 4.0);
   return saturate(float3(R,G,B));
}

float RGBCVtoHUE(in float3 RGB, in float C, in float V)
{
     float3 Delta = (V - RGB) / C;
     Delta.rgb -= Delta.brg;
     Delta.rgb += float3(2.0,4.0,6.0);
     Delta.brg = step(V, RGB) * Delta.brg;
     float H;
     H = max(Delta.r, max(Delta.g, Delta.b));
     return frac(H / 6.0);
}

float3 HSVtoRGB(in float3 HSV)
{
   float3 RGB = HUEtoRGB(HSV.x);
   return ((RGB - 1) * HSV.y + 1) * HSV.z;
}
 
float3 RGBtoHSV(in float3 RGB)
{
   float3 HSV = 0.0;
   HSV.z = max(RGB.r, max(RGB.g, RGB.b));
   float M = min(RGB.r, min(RGB.g, RGB.b));
   float C = HSV.z - M;
   if (C != 0.0)
   {
     HSV.x = RGBCVtoHUE(RGB, C, HSV.z);
     HSV.y = C / HSV.z;
   }
   return HSV;
}
#endif

#ifdef FILMICPASS
float GetLuminance(float3 sample)
	 {
		 return dot(sample.xyz, 0.333);
	 }
	 
float4 FilmPass(float4 B)
{
	float4 G = B;
	float4 H = 0.01;
 
	B = pow(abs(B), Linearization);
	B = lerp(H, B, Contrast);
 
	float A = GetLuminance(B.rgb);
	float4 D = A;
 
	B = pow(abs(B), 1.0 / BaseGamma);
 
	float a = RedCurve;
	float b = GreenCurve;
	float c = BlueCurve;
	float d = BaseCurve;
 
	float y = 1.0 / (1.0 + exp(a / 2.0));
	float z = 1.0 / (1.0 + exp(b / 2.0));
	float w = 1.0 / (1.0 + exp(c / 2.0));
	float v = 1.0 / (1.0 + exp(d / 2.0));
 
	float4 C = B;
 
	D.r = (1.0 / (1.0 + exp(-a * (D.r - 0.5))) - y) / (1.0 - 2.0 * y);
	D.g = (1.0 / (1.0 + exp(-b * (D.g - 0.5))) - z) / (1.0 - 2.0 * z);
	D.b = (1.0 / (1.0 + exp(-c * (D.b - 0.5))) - w) / (1.0 - 2.0 * w);
 
	D = pow(abs(D), 1.0 / EffectGamma);
 
	float4 Di = 1.0 - D;
 
	D = lerp(D, Di, Bleach);
 
	D.r = pow(abs(D.r), 1.0 / EffectGammaR);
	D.g = pow(abs(D.g), 1.0 / EffectGammaG);
	D.b = pow(abs(D.b), 1.0 / EffectGammaB);
 
	if (D.r < 0.5)
		C.r = (2.0 * D.r - 1.0) * (B.r - B.r * B.r) + B.r;
	else
		C.r = (2.0 * D.r - 1.0) * (sqrt(B.r) - B.r) + B.r;
 
	if (D.g < 0.5)
		C.g = (2.0 * D.g - 1.0) * (B.g - B.g * B.g) + B.g;
	else
		C.g = (2.0 * D.g - 1.0) * (sqrt(B.g) - B.g) + B.g;
 	//if (AgainstAllAutority) then fckoff else fckoff
	if (D.b < 0.5)
		C.b = (2.0 * D.b - 1.0) * (B.b - B.b * B.b) + B.b;
	else
		C.b = (2.0 * D.b - 1.0) * (sqrt(B.b) - B.b) + B.b;
 
	float4 F = lerp(B, C, Strenght);
 
	F = (1.0 / (1.0 + exp(-d * (F - 0.5))) - v) / (1.0 - 2.0 * v);
 
	float r2R = 1.0 - Saturation;
	float g2R = 0.0 + Saturation;
	float b2R = 0.0 + Saturation;
 
	float r2G = 0.0 + Saturation;
	float g2G = (1.0 - Fade) - Saturation;
	float b2G = (0.0 + Fade) + Saturation;
 
	float r2B = 0.0 + Saturation;
	float g2B = (0.0 + Fade) + Saturation;
	float b2B = (1.0 - Fade) - Saturation;
 
	float4 iF = F;
 
	F.r = (iF.r * r2R + iF.g * g2R + iF.b * b2R);
	F.g = (iF.r * r2G + iF.g * g2G + iF.b * b2G);
	F.b = (iF.r * r2B + iF.g * g2B + iF.b * b2B);
 
	float N = GetLuminance(F.rgb);
	float4 Cn = F;
 
	if (N < 0.5)
		Cn = (2.0 * N - 1.0) * (F - F * F) + F;
	else
		Cn = (2.0 * N - 1.0) * (sqrt(F) - F) + F;
 
	Cn = pow(abs(Cn), 1.0 / Linearization);
 
	float4 Fn = lerp(B, Cn, Strenght);
	return Fn;
}
#endif
//--------------------------------------------------------------------------------------
// Textures
//--------------------------------------------------------------------------------------
texture2D texColor;
texture2D texDepth;
texture2D texNoise;
//--------------------------------------------------------------------------------------
// Sampler Inputs
//--------------------------------------------------------------------------------------


sampler2D InputSampler = sampler_state
{
    Texture = (texColor);
    MinFilter = POINT;
    MagFilter = POINT;
    MipFilter = POINT;
    AddressU   = Clamp;
	AddressV   = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerDepth = sampler_state
{
	Texture   = <texDepth>;
	MinFilter = POINT;
	MagFilter = POINT;
	MipFilter = NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerNoise = sampler_state
{
	Texture   = <texNoise>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = LINEAR;
	AddressU  = Wrap;
	AddressV  = Wrap;
	SRGBTexture = FALSE;
	MaxMipLevel = 0;
	MipMapLodBias = 0;
};

struct VS_OUTPUT_POST
{
	float4 vpos  : POSITION;
	float2 txcoord : TEXCOORD0;
};

struct VS_INPUT_POST
{
	float3 pos  : POSITION;
	float2 txcoord : TEXCOORD0;
};


//--------------------------------------------------------------------------------------
// Vertex Shader Input
//--------------------------------------------------------------------------------------

VS_OUTPUT_POST VS_PostProcess(VS_INPUT_POST IN)
{
	VS_OUTPUT_POST OUT;

	float4 pos=float4(IN.pos.x,IN.pos.y,IN.pos.z,1.0);

	OUT.vpos=pos;
	OUT.txcoord.xy=IN.txcoord.xy;

	return OUT;
}

//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------


float Luminance( float3 c )
{
	return dot( c, float3(0.22, 0.707, 0.071) );
}
#ifdef DOF
float penta(float2 coords) //pentagonal shape
{
	float scale = float(rings) - 1.3;
	float4  HS0 = float4( 1.0,         0.0,         0.0,  1.0);
	float4  HS1 = float4( 0.309016994, 0.951056516, 0.0,  1.0);
	float4  HS2 = float4(-0.809016994, 0.587785252, 0.0,  1.0);
	float4  HS3 = float4(-0.809016994,-0.587785252, 0.0,  1.0);
	float4  HS4 = float4( 0.309016994,-0.951056516, 0.0,  1.0);
	float4  HS5 = float4( 0.0        ,0.0         , 1.0,  1.0);
	
	float4  one = float4( 1,1,1,1 );
	
	float4 P = float4(coords,float2(scale, scale)); 
	
	float4 dist = float4(0,0,0,0);
	float inorout = -4.0;
	
	dist.x = dot( P, HS0 ); //by Marty McFly
	dist.y = dot( P, HS1 );
	dist.z = dot( P, HS2 );
	dist.w = dot( P, HS3 );
	
	dist = smoothstep( -feather, feather, dist );
	
	inorout += dot( dist, one );
	
	dist.x = dot( P, HS4 );
	dist.y = HS5.w - abs( P.z );
	
	dist = smoothstep( -feather, feather, dist );
	inorout += dist.x;
	
	return saturate( inorout );
}


float linearize(float depth)
{
	return -zfar * znear / (depth * (zfar - znear) - zfar);
}

float2 rand(float2 coord) //generating noise/pattern texture for dithering
{
	float noiseX = ((frac(1.0-coord.x*(screenRes.x/2.0))*0.25)+(frac(coord.y*(screenRes.y/2.0))*0.75))*2.0-1.0;
	float noiseY = ((frac(1.0-coord.x*(screenRes.x/2.0))*0.75)+(frac(coord.y*(screenRes.y/2.0))*0.25))*2.0-1.0;
	
	return float2(noiseX,noiseY);
}

float4 colorDof(float2 coords,float blur) //processing the sample
{
	float4 colDF = float4(0,0,0,1);
	
	colDF.x = tex2D(InputSampler,coords + float2(0.0,1.0)*texel*fringe*blur).x;
	colDF.y = tex2D(InputSampler,coords + float2(-0.866,-0.5)*texel*fringe*blur).y;
	colDF.z = tex2D(InputSampler,coords + float2(0.866,-0.5)*texel*fringe*blur).z; //by Marty McFly
	
	float3 lumcoeff = float3(0.299,0.587,0.114);
	float lum = dot(colDF.xyz,lumcoeff);
	float thresh = max((lum-threshold)*gain, 0.0);
	float3 nullcol = float3(0,0,0);
	colDF.xyz +=lerp(nullcol,colDF.xyz,thresh*blur);
	return colDF;
}

float vignette(float2 coord)
{
	float dist = distance(coord, float2(0.5,0.5));
	dist = smoothstep(vignout+(fstop/MartyMcFly), vignin+(fstop/MartyMcFly), dist);
	return saturate(dist);
}
#endif

#ifdef NOISE

float Random(float2 co)
{
    return frac(sin(dot(co.xy, float2(12.9898, 78.233))) * 43758.5453);
}

float Grain(float3 tex)
{
	float PI = 3.1415926535897932384626433832795;
	float rofl = Random(tex.xy);
	float grain = sin(PI * tex.z * rofl * fGrainFreq) * fGrainScale * rofl;
	return grain;
}
#endif

float4 main(VS_OUTPUT_POST i, float2 vPos : VPOS) : COLOR
{
    float4 color = tex2D(InputSampler, i.txcoord);

//color.xyz = max(color.xyz, 0.00);




#ifdef HSVULTIMATE
#ifdef ENABLE_HSV_CONVERSION
float3 hsvcolor = RGBtoHSV( color.xyz );

#ifdef ENABLE_POW_CURVE
#ifdef ENABLE_HUE_ADJUSTMENT
hsvcolor.x = EColorHueMod + ( EColorHueMult * pow( hsvcolor.x, EColorHuePow ) );
#endif
#ifdef ENABLE_SATURATION_ADJUSTMENT
hsvcolor.y = EColorSaturationMod + ( EColorSaturationMult * pow( hsvcolor.y, EColorSaturationPow ) );
#endif
#ifdef ENABLE_INTENSITY_ADJUSTMENT
hsvcolor.z = EColorIntensityMod + ( EColorIntensityMult * pow( hsvcolor.z, EColorIntensityPow ) );
#endif
#ifdef ENABLE_COLOR_EQUALIZER
hsvcolor.y = ColorEqualizerMod( hsvcolor.x ) + ( ColorEqualizerMult( hsvcolor.x ) * pow( hsvcolor.y, ColorEqualizerPow( hsvcolor.x ) ) );
#endif
#endif

#ifndef ENABLE_POW_CURVE
#ifdef ENABLE_HUE_ADJUSTMENT
hsvcolor.x = EColorHueMod + ( EColorHueMult * hsvcolor.x );
#endif
#ifdef ENABLE_SATURATION_ADJUSTMENT
hsvcolor.y = EColorSaturationMod + ( EColorSaturationMult * hsvcolor.y );
#endif
#ifdef ENABLE_INTENSITY_ADJUSTMENT
hsvcolor.z = EColorIntensityMod + ( EColorIntensityMult * hsvcolor.z );
#endif
#ifdef ENABLE_COLOR_EQUALIZER
hsvcolor.y = ColorEqualizerMod( hsvcolor.x ) + ( ColorEqualizerMult( hsvcolor.x ) * hsvcolor.y );
#endif
#endif

hsvcolor.y = max( hsvcolor.y, 0.0 );
hsvcolor.z = max( hsvcolor.z, 0.0 );
color.xyz = HSVtoRGB( hsvcolor );
#endif
#endif


#ifdef GAMMACORRECTION
color = pow(color, 1/GammaAmount);
#endif

#ifdef COLORCORRMAIN
color.r = (color.r)*(0.05 + 1.0f) + (color.g + color.b)*(-0.04); 
color.g = (color.g)*(0.06 + 1.0f) + (color.r + color.b)*(-0.04);
color.b = (color.b)*(0.055 + 1.0f) + (color.r + color.g)*(-0.04);

color.rgb = (color.rgb * (1.0 + color.rgb / 2))/(color.rgb + 4);
color = color*(1.0f + 0.2) - 0.12;
#endif


#ifdef COLORCORRMAIN
color = saturate(color);
color = 1 - (0.97 - 0.065)*(1.04 - color);
color = saturate(color);
color = color + (0.82f / 2.0 - 0.5) * (1.0 - color);
color = saturate(color);
color.rgb = pow(0.85f * color, 1.0 / 0.95f); 

float dbr = -color.r + 1.4f;
float dbg = -color.g + 1.4f;
float dbb = -color.b + 1.4f;

float pr = lerp(dbr, 0.55f, 0.66f);
float pg = lerp(dbg, 0.55f, 0.66f);
float pb = lerp(dbb, 0.55f, 0.66f);

color.r = pow((color.r * 0.94f - 0.0035f), pr);
color.g = pow((color.g * 0.94f - 0.002f), pg);
color.b = pow((color.b * 0.91f + 0.001f), pb);

color = color * 4.0;
#endif

#ifdef FILMICCURVE
float3 curr = Uncharted2Tonemap(color);
float3 whiteScale = Uncharted2Tonemap(FilmWeight);
color.xyz = curr*whiteScale;
color = pow(color, 0.55);
color = color*FilmicExposure;
#endif

#ifdef REINHARD_ICE21
float pixelLumi = dot(color.xyz, LUM_FACTOR.xyz);
float toneMapCalc = pixelLumi / (pixelLumi + 1);
color =  toneMapCalc * pow(color / pixelLumi, REINHARD_SAT);
color = color * TONEMAPEXP;
#endif

#ifdef CROSSPROCESS

		float4 image1 = color;
		float4 image2 = color;
		float gray = dot(float3(0.5,0.5,0.5), image1);

		image1 = lerp (gray, image1,CrossSaturation2);

		image1 = lerp (0.35, image1,CrossContrast);

		image1 +=CrossBrightness;
		image1 *=1.2;

		image2.r = image1.r * crossMatrix[0].x + crossMatrix[0].y;
		image2.g = image1.g * crossMatrix[1].x + crossMatrix[1].y;
		image2.b = image1.b * crossMatrix[2].x + crossMatrix[2].y;

		color = lerp(image1, image2, CrossAmount);

#endif

#ifdef CROSSICE
float Weight;
float4 colInput = color;
float4 colMood = 1.0f;
colMood.r = moodR;
colMood.g = moodG;
colMood.b = moodB;
float fLum = ( colInput.r + colInput.g + colInput.b ) / 3;
colMood = lerp(0, colMood, saturate(fLum * 2.0));
colMood = lerp(colMood, 1, saturate(fLum - 0.5) * 2.0);
float4 colOutput = lerp(colInput, colMood, saturate(fLum * fRatio));
color.rgb=max(0, colOutput);
#endif

#ifdef HDR
color.xyz = max(0, color.xyz - Defog * FogColor.xyz);
color.xyz *= pow(2.0f, Exposure);
color.xyz = pow(color.xyz, Gamma);
float3 trollface = color.xyz * float3(1.05f, 0.97f, 1.27f);
color.xyz = lerp(color.xyz, trollface, BlueShift);
#endif

#ifdef FILMICPASS
color = FilmPass(color);
#endif

#ifdef VIBRANCEPASS
float3 lumCoeff = float3(0.2126, 0.7152, 0.0722);
float vibranceluma = dot(lumCoeff, color.xyz);

float max_color = max(color.x, max(color.y,color.z)); //Find the strongest color
float min_color = min(color.x, max(color.y,color.z)); //Find the weakest color
float color_saturation = max_color - min_color;

color.xyz = lerp(vibranceluma, color.xyz, (1.0 + (Vibrance * (1.0 - (sign(Vibrance) * color_saturation)))));
#endif

#ifdef BORISVIGNETTE
	float2	uv=(i.txcoord-0.5)*EVignetteRadius;
	float	vignetteold=saturate(dot(uv.xy, uv.xy));
	vignetteold=pow(vignetteold, EVignetteCurve);
	#ifdef VIGNCOLORING
	float3	EVignetteColor=float3(VIGNREDAMOUNT, VIGNGREENAMOUNT, VIGNBLUEAMOUNT);
	#else
	float3	EVignetteColor=float3(0.0, 0.0, 0.0);
	#endif
	color.xyz=lerp(color.xyz, EVignetteColor, vignetteold*EVignetteAmount);
#endif

#ifdef HD6_VIGNETTE

	float rovigpwr = CircularPower; //for a circular vignette
	float2 sqvigpwr = float2( SquareTop, SquareBottom ); // for the top and bottom of the screen
	float vsatstrength = ColorDistortion; // color distortion
	float vignettepow = ContrastSharpen; // increases the contrast and sharpness
	float vstrengthatnight = VignetteBorder;
 
 	float2 inTex = i.txcoord;
 	float vhnd = 0.5;
 	float4 voriginal = color;
 	float4 vcolor = voriginal;
 	vcolor.xyz=1;
 	inTex -= 0.5; // center
 	inTex.y += 0.01; // offset from the center
 	float vignette = 1.0 - dot( inTex, inTex );
 	vcolor *= pow( vignette, vignettepow );
 
 	float4 rvigtex = vcolor;
 	rvigtex.xyz = pow( vcolor, 1 );
 	rvigtex.xyz = lerp(float3(0.5, 0.5, 0.5), rvigtex.xyz, 2.25); // contrast
 	rvigtex.xyz = lerp(float3(1,1,1),rvigtex.xyz,rovigpwr); // strength of the circular vinetty
 
//darken the top and bottom
 	float4 vigtex = vcolor;
 	vcolor.xyz = float3(1,1,1);

#ifdef LEFTANDRIGHT
 	float3 topv = min((inTex.x+0.5)*2,1.5) * 2; // top
 	float3 botv = min(((0-inTex.x)+0.5)*2,1.5) * 2; // botton
	topv= lerp(float3(1,1,1), topv, sqvigpwr.x);
 	botv= lerp(float3(1,1,1), botv, sqvigpwr.y);
	vigtex.xyz = (topv)*(botv);
#endif
#ifdef TOPANDBOTTOM
        float3 topv = min((inTex.y+0.5)*2,1.5) * 2; // top
 	float3 botv = min(((0-inTex.y)+0.5)*2,1.5) * 2; // botton
	topv= lerp(float3(1,1,1), topv, sqvigpwr.x);
 	botv= lerp(float3(1,1,1), botv, sqvigpwr.y);
	vigtex.xyz = (topv)*(botv);
#endif
#ifdef CORNERDARKEN
	float3 rightv = min((inTex.x+0.5)*2,1.5) * 2;
 	float3 leftv = min(((0-inTex.x)+0.5)*2,1.5) * 2; 
        float3 topv = min((inTex.y+0.5)*2,1.5) * 2; 
 	float3 botv = min(((0-inTex.y)+0.5)*2,1.5) * 2; 
 	rightv= lerp(float3(1,1,1), rightv, sqvigpwr.y);
 	leftv= lerp(float3(1,1,1), leftv, sqvigpwr.x);
        topv= lerp(float3(1,1,1), topv, sqvigpwr.x);
 	botv= lerp(float3(1,1,1), botv, sqvigpwr.y);
 	vigtex.xyz = (topv)*(botv)*(rightv)*(leftv);
#endif
 	
 	
 	//vigtex.xyz = lerp(float3(1,1,1),vigtex.xyz,sqvigpwr); // strength of the top and bottom
 
 // mix the two types of vignettes
 	vigtex.xyz*=rvigtex.xyz;
	vigtex.xyz = lerp(vigtex.xyz,float3(1,1,1),(vhnd-vstrengthatnight*vhnd)); //for a dark screen
 	vigtex.xyz = min(vigtex.xyz,1);
 	vigtex.xyz = max(vigtex.xyz,0);
 	float3 vtintensity = dot(voriginal.xyz, float3(0.2125, 0.7154, 0.0721));
 	color.xyz = lerp(vtintensity, voriginal.xyz, ((((1-(vigtex.xyz*2))+2)-1)*vsatstrength)+1 );
  	color.xyz *= (vigtex.xyz);
#endif





float2 InputSize = float2(sxres, syres/aspect);
float Amount = sharps;
float2 offset = offsetv / InputSize;
float4 sharpencolor=tex2D(InputSampler, i.txcoord);
sharpencolor += tex2D(InputSampler, i.txcoord - offset) * Amount;
sharpencolor -= tex2D(InputSampler, i.txcoord + offset) * Amount;


#ifdef USE_DITHER
float dither_size = 2.0;  //move to settings?
float dither_bit  = 8.0;  //move to settings?
float grid_position = frac(dot(i.txcoord,(screen_size / dither_size)) + (0.5 / dither_size)); 
float dither_shift = (0.25) * (1.0 / (pow(2,dither_bit) - 1.0));
color.xyz += lerp(2.0 * dither_shift,(-2.0 * dither_shift),grid_position); 
#endif


color.a = 1.0f;

return color*sharpencolor;
}

#ifdef LIGHTSCATTERING
float4 PS_ProcessSunShafts(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR 
{	
	float2 texCoord = IN.txcoord;
	// Calculate vector from pixel to light source in screen space.  
    half2 deltaTexCoord = (texCoord - ScreenLightPos.xy);  
    // Divide by number of samples and scale by control factor.  
    deltaTexCoord *= 1.0f / NUM_SAMPLES * Density;  
    // Store initial sample.  
    half3 color = max(float4(0,0,0,0),tex2D(InputSampler, texCoord)-SStresh);  
	// Set up illumination decay factor.  
	half illuminationDecay = 1.0f;  
	// Evaluate summation from Equation 3 NUM_SAMPLES iterations.  
	for (int i = 0; i < NUM_SAMPLES; i++)  
	{  
		// Step sample location along ray.  
		texCoord -= deltaTexCoord;  
		// Retrieve sample at new location.  
	    half3 sample = max(float4(0,0,0,0),tex2D(InputSampler, texCoord)-SStresh);  
		// Apply sample attenuation scale/decay factors.  

#ifdef TINTCOLOR
                sample.r *= REDAMOUNT;
		sample.g *= GREENAMOUNT;
		sample.b *= BLUEAMOUNT;
#endif

#ifdef COLORING
sample = dot(sample, float3(0.3, 0.59, 0.11));
                sample.r *= REDAMOUNT;
		sample.g *= GREENAMOUNT;
		sample.b *= BLUEAMOUNT;
#endif

		sample *= illuminationDecay * Weight;  
		// Accumulate combined color.  
		color += sample;  
		// Update exponential decay factor.  
		illuminationDecay *= Decay;  
	}
	// Output final color with a further scale control factor.  
	return float4( saturate(color*SunExposure) , color.x);
}
#endif


#ifdef ANAMORPHICFLARE
float4 CCShadow (VS_OUTPUT_POST i, float2 vPos : VPOS) : COLOR
{

float4 threshhold1 = {thresh1, 1/(1-thresh1), 5.0, 5.0}; 
float4 threshhold2 = {thresh2, 1/(1-thresh2), 5.0, 5.0};
	float4 col = tex2D(InputSampler, i.txcoord);
	//float4 color = tex2Dlod(InputSampler, float4(i.txcoord,0,8));
	
    float4 sum = float4(0,0,0,0);
 
    // blur this in y (vertical)
    // also I downsample it to get interesting result 

#ifdef FLAREMODE_1
sum += tex2Dlod(InputSampler, float4(i.txcoord.y - 4.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.05;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y - 3.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.09;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y - 2.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.12;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y - fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.15;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y, i.txcoord.y,0,fBlurDownsampling)) * 0.16;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.15;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + 2.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.12;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + 3.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.09;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + 4.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.05;
#endif
#ifdef FLAREMODE_2
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 100.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.01;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 99.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.01;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 98.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.02;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 97.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.03;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 96.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.04;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 95.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.05;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 94.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.06;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 93.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.07;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 92.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.08;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 91.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.09;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 90.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.10;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 89.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.11;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 88.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.12;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 87.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.13;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 86.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.14;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 85.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.15;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 84.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.16;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 83.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.17;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 82.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.18;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 81.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.19;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 80.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.20;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 79.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.21;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 78.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.22;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 77.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.23;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 76.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.24;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 75.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.25;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 74.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.26;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 73.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.27;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 72.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.28;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 71.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.29;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 70.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.30;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 69.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.31;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 68.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.32;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 67.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.33;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 66.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.34;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 65.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.35;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 64.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.36;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 63.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.37;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 62.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.38;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 61.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.39;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 60.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.40;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 59.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.41;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 58.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.42;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 57.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.43;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 56.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.44;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 55.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.45;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 54.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.46;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 53.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.47;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 52.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.48;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 51.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.49;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 50.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.50;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 49.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.51;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 48.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.52;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 47.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.53;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 46.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.54;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 45.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.55;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 44.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.56;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 43.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.57;
//------------------------------------------------------------------------------------------------------------------------------
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 42.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.58;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 41.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.59;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 40.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.60;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 39.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.61;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 38.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.62;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 37.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.63;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 36.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.64;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 35.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.65;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 34.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.66;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 33.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.67;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 32.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.68;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 31.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.69;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 30.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.70;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 29.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.71;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 28.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.72;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 27.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.73;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 26.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.74;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 25.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.75;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 24.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.76;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 23.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.77;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 22.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.78;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 21.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.79;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 20.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.80;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 19.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.81;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 18.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.82;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 17.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.83;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 16.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.84;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 15.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.85;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 14.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.86;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 13.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.87;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 12.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.88;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 11.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.89;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 10.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.90;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 9.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.91;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 8.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.92;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 7.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.93;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 6.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.94;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 5.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.95;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 4.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.96;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 3.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.97;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - 2.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.98;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x - fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.99;


sum += tex2Dlod(InputSampler, float4(i.txcoord.x, i.txcoord.y,0,fBlurDownsampling)) * 1.0;


sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 100.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.01;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 99.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.01;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 98.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.02;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 97.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.03;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 96.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.04;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 95.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.05;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 94.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.06;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 93.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.07;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 92.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.08;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 91.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.09;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 90.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.10;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 89.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.11;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 88.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.12;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 87.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.13;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 86.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.14;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 85.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.15;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 84.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.16;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 83.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.17;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 82.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.18;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 81.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.19;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 80.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.20;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 79.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.21;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 78.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.22;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 77.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.23;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 76.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.24;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 75.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.25;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 74.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.26;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 73.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.27;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 72.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.28;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 71.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.29;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 70.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.30;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 69.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.31;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 68.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.32;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 67.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.33;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 66.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.34;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 65.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.35;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 64.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.36;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 63.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.37;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 62.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.38;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 61.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.39;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 60.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.40;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 59.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.41;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 58.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.42;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 57.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.43;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 56.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.44;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 55.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.45;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 54.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.46;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 53.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.47;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 52.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.48;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 51.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.49;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 50.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.50;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 49.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.51;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 48.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.52;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 47.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.53;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 46.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.54;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 45.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.55;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 44.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.56;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 43.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.57;
//+++++-------------------------------------------------------------------------------------------------------------------------
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 42.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.58;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 41.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.59;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 40.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.60;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 39.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.61;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 38.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.62;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 37.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.63;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 36.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.64;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 35.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.65;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 34.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.66;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 33.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.67;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 32.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.68;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 31.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.69;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 30.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.70;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 29.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.71;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 28.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.72;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 27.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.73;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 26.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.74;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 25.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.75;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 24.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.76;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 23.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.77;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 22.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.78;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 21.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.79;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 20.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.80;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 19.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.81;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 18.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.82;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 17.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.83;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 16.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.84;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 15.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.85;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 14.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.86;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 13.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.87;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 12.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.88;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 11.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.89;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 10.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.90;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 9.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.91;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 8.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.92;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 7.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.93;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 6.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.94;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 5.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.95;

sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 4.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.96;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 3.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.97;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + 2.0*fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.98;
sum += tex2Dlod(InputSampler, float4(i.txcoord.x + fBlurSize, i.txcoord.y,0,fBlurDownsampling)) * 0.99;

//vertical for smoothing
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y - 10.0*fBlurSize, i.txcoord.x,0,fBlurDownsampling)) * 0.1;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y - 6.6*fBlurSize, i.txcoord.x,0,fBlurDownsampling)) * 0.25;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y - 3.3*fBlurSize, i.txcoord.x,0,fBlurDownsampling)) * 0.50;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y - fBlurSize, i.txcoord.x,0,fBlurDownsampling)) * 0.75;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y, i.txcoord.x,0,fBlurDownsampling)) * 1.0;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + fBlurSize, i.txcoord.x,0,fBlurDownsampling)) * 0.75;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + 3.3*fBlurSize, i.txcoord.x,0,fBlurDownsampling)) * 0.50;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + 6.6*fBlurSize, i.txcoord.x,0,fBlurDownsampling)) * 0.25;
    sum += tex2Dlod(InputSampler, float4(i.txcoord.y + 10.0*fBlurSize, i.txcoord.x,0,fBlurDownsampling)) * 0.1;

#endif




sum = dot(sum, float3(0.3, 0.59, 0.11));

#ifdef ANAMORPHICTINTCOLOR
sum.r = sum.r * ANAMORPHICREDAMOUNT;
sum.g = sum.g * ANAMORPHICGREENAMOUNT;
sum.b = sum.b * ANAMORPHICBLUEAMOUNT;
#endif	


	// do some cool stuff to get bloom...
#ifdef FLAREMODE_1
	float4 color = max(float4(0.0,0.0,0.0,0.0), sum-threshhold1.x);
	float4 toBlend = saturate (color * fFlareIntensity1);
#else
        float4 color = max(float4(0.0,0.0,0.0,0.0), sum-threshhold2.x);
	float4 toBlend = saturate (color * fFlareIntensity2);
#endif
		
	
	
	float4 smpl = 1-(1-col)*(1-toBlend);
	
	float factor = max(smpl.x, max(smpl.y, smpl.z));
	float factorM = (smpl.x + smpl.y + smpl.z)/3;
	float4 shadows = paramsS;
	float4 midtones = paramsM;
	float4 highlights = paramsH;
	
	float4 Color;
	
	if(factor < 0.1)	//Shadows
	{
		if(factor > 0.01)
		{
			factor = (factor + 0.09)*10;
			
			shadows.w = shadows.x; //.w is value backup
			shadows.x = ((1 - shadows.w) / 2) * factor;
			shadows.x += shadows.w;	//Adding backup
			
			shadows.w = shadows.y; //.w is value backup
			shadows.y = ((1 - shadows.w) / 2) * factor;
			shadows.y += shadows.w;	//Adding backup
			
			shadows.w = shadows.z; //.w is value backup
			shadows.z = ((1 - shadows.w) / 2) * factor;
			shadows.z += shadows.w;	//Adding backup
		}
		Color = float4(smpl.x * shadows.x, smpl.y * shadows.y, smpl.z * shadows.z, smpl.w);	
	}
	else if(factorM >= 0.1 && factorM <= 0.5)	//Middle-tones
	{
		if(factorM > 0.3)
		{
			factorM = (factorM - 0.31)*10;
			
			midtones.w = midtones.x; //.w is value backup
			midtones.x = ((1 - midtones.w) / 2) * factorM;
			midtones.x += midtones.w;	//Adding backup
			
			midtones.w = midtones.y; //.w is value backup
			midtones.y = ((1 - midtones.w) / 2) * factorM;
			midtones.y += midtones.w;	//Adding backup
			
			midtones.w = midtones.z; //.w is value backup
			midtones.z = ((1 - midtones.w) / 2) * factorM;
			midtones.z += midtones.w;	//Adding backup
		}
		else if(factorM <= 0.3)
		{
			factorM = (factorM - 0.1)*10;
			
			midtones.w = midtones.x;
			midtones.x = (1 - midtones.w) - (factorM * ((1 - midtones.w) / 2));
			midtones.x += midtones.w;
			
			midtones.w = midtones.y;
			midtones.y = (1 - midtones.w) - (factorM * ((1 - midtones.w) / 2));
			midtones.y += midtones.w;
			
			midtones.w = midtones.z;
			midtones.z = (1 - midtones.w) - (factorM * ((1 - midtones.w) / 2));
			midtones.z += midtones.w;
		}
		
		Color = float4(smpl.x * midtones.x, smpl.y * midtones.y, smpl.z * midtones.z, smpl.w);
	}		
	else if(factorM > 0.5)	//Hightlights
	{
		if(factorM <= 0.8)
		{
			factorM = (factorM - 0.51)*10;
			
			highlights.w = highlights.x;
			highlights.x = (1 - highlights.w) - (factorM * ((1 - highlights.w) / 2));
			highlights.x += highlights.w;
			
			highlights.w = highlights.y;
			highlights.y = (1 - highlights.w) - (factorM * ((1 - highlights.w) / 2));
			highlights.y += highlights.w;
			
			highlights.w = highlights.z;
			highlights.z = (1 - highlights.w) - (factorM * ((1 - highlights.w) / 2));
			highlights.z += highlights.w;
		}
	
		Color = float4(smpl.x * highlights.x, smpl.y * highlights.y, smpl.z * highlights.z, smpl.w);
	}
	else Color = smpl;
	
	float lum = Luminance(Color.xyz);
return Color;
	
}
#endif

#ifdef DOF
float4 PS_ProcessDoFBokeh(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR 
{
	float depth = linearize(tex2D(SamplerDepth,IN.txcoord.xy).x);
	
	float fDepth = focalDepth;
	
	if (autofocus)
	{
		fDepth = linearize(tex2D(SamplerDepth,focus).x);
	}
	
	float blur = 0.0;
	if (manualdof)
	{    
		float a = depth-fDepth; //focal plane
		float b = (a-fdofstart)/fdofdist; //far DoF
		float c = (-a-ndofstart)/ndofdist; //near Dof
		blur = (a>0.0)?b:c;
	}
	else
	{
		float f = focalLength; //focal length in mm
		float d = fDepth*1000.0; //focal plane in mm
		float o = depth*1000.0; //depth in mm
		
		float a = (o*f)/(o-f); 
		float b = (d*f)/(d-f); 
		float c = (d-f)/(d*fstop*CoC); 
		
		blur = abs(a-b)*c;
	}
	blur = saturate(blur);
	float2 noise = rand(IN.txcoord.xy)*namount*blur;
	
	float w = (1.0/screenRes.x)*blur*maxblur+noise.x;
	float h = (1.0/screenRes.y)*blur*maxblur+noise.y;
	
	float4 col = float4(0,0,0,1);
	
	if(blur < 0.05) //some optimization thingy
	{
		col = tex2D(InputSampler, IN.txcoord.xy);
	}
	else
	{
	col = tex2D(InputSampler, IN.txcoord.xy);
	float s = 1.0;
	int ringsamples;
	for (int i = 1; i <= rings; i += 1)
	{
		ringsamples = i * samples;
		for (int j = 0 ; j < ringsamples ; j += 1)
		{
			float step = PI*2.0 / ringsamples;
			float pw = cos(j*step)*i;
			float ph = sin(j*step)*i;
			float p = 1.0;
			if (pentagon)
			{ 
				p = penta(float2(pw,ph));
			}
			col.xyz += colorDof(IN.txcoord.xy + float2(pw*w,ph*h),blur).xyz;  
			s += 1.0*lerp(1.0,i/rings,bias)*p;
		}
	}
	col = col/s; //divide by sample count
	}
	
	if (vignetting)
	{
		col *= vignette(IN.txcoord.xy);
	}
	
	return col;
}
#endif



#ifdef CHROMATICABBERATION
float4 PS_PostProcessChroma(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{

float4 coord=0.0;

coord.xy=IN.txcoord.xy;
coord.w=0.0;

float3 eta = float3(1.0+ChromaticAmount*0.9,1.0+ChromaticAmount*0.6,1.0+ChromaticAmount*0.3);
float2 center;
center.x = coord.x-0.5;
center.y = coord.y-0.5;
float LensZoom = 1.0/LensSize;

float r2 = (IN.txcoord.x-0.5) * (IN.txcoord.x-0.5) + (IN.txcoord.y-0.5) * (IN.txcoord.y-0.5);     
float f = 0;

if( LensDistortionCubic == 0.0){
	f = 1 + r2 * LensDistortion;
}else{
                f = 1 + r2 * (LensDistortion + LensDistortionCubic * sqrt(r2));
};

float x = f*LensZoom*(coord.x-0.5)+0.5;
float y = f*LensZoom*(coord.y-0.5)+0.5;
float2 rCoords = (f*eta.r)*LensZoom*(center.xy*0.5)+0.5;
float2 gCoords = (f*eta.g)*LensZoom*(center.xy*0.5)+0.5;
float2 bCoords = (f*eta.b)*LensZoom*(center.xy*0.5)+0.5;

//float3 inputDistord = tex2D(InputSampler,float2(x,y));
float4 inputDistord = float4(tex2D(InputSampler,rCoords).r , tex2D(InputSampler,gCoords).g ,tex2D(InputSampler,bCoords).b, tex2D(InputSampler,float2(x,y)).a);
return float4(inputDistord.r,inputDistord.g,inputDistord.b,1);
}
#endif



float ScreenSizeFXAA = 1920;; //width of the display resolution (1920 f.e.)
float ScreenScaleYFXAA = 1.333; //screen proportions (1.333 for 1920/1080)

float4 FxaaPixelShader(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{  

#define FxaaTexTop(t, p) tex2Dlod(t, float4(p, 0.0, 0.0))
#define FxaaTexOff(t, p, o, r) tex2Dlod(t, float4(p + (o * r), 0, 0))

float2 pos = IN.txcoord.xy;

float2 rcpFrame = float2(1/ScreenSizeFXAA, ScreenScaleYFXAA/ScreenSizeFXAA);
float4 rcpFrameOpt = float4(2/ScreenSizeFXAA, 2*ScreenScaleYFXAA/ScreenSizeFXAA, 0.5/ScreenSizeFXAA, 0.5*ScreenScaleYFXAA/ScreenSizeFXAA);

float lumaN = dot(FxaaTexOff(InputSampler, pos.xy, float2(0, -1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaW = dot(FxaaTexOff(InputSampler, pos.xy, float2(-1, 0), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));


float4 rgbyM;
rgbyM.xyz = FxaaTexTop(InputSampler, pos.xy).xyz;
rgbyM.w = dot(rgbyM.xyz, float3(0.299, 0.587, 0.114));
float lumaE = dot(FxaaTexOff(InputSampler, pos.xy, float2( 1, 0), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaS = dot(FxaaTexOff(InputSampler, pos.xy, float2( 0, 1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaM = rgbyM.w;


float rangeMin = min(lumaM, min(min(lumaN, lumaW), min(lumaS, lumaE)));
float rangeMax = max(lumaM, max(max(lumaN, lumaW), max(lumaS, lumaE)));
float range = rangeMax - rangeMin;

if(range < max(FXAA_QUALITY__EDGE_THRESHOLD_MIN, rangeMax * FXAA_QUALITY__EDGE_THRESHOLD)) return rgbyM;


float lumaNW = dot(FxaaTexOff(InputSampler, pos.xy, float2(-1,-1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaNE = dot(FxaaTexOff(InputSampler, pos.xy, float2( 1,-1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaSW = dot(FxaaTexOff(InputSampler, pos.xy, float2(-1, 1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));
float lumaSE = dot(FxaaTexOff(InputSampler, pos.xy, float2( 1, 1), rcpFrame.xy).xyz, float3(0.299, 0.587, 0.114));



float lumaL = (lumaN + lumaW + lumaE + lumaS) * 0.25;
float rangeL = abs(lumaL - lumaM);
float blendL = saturate((rangeL / range) - FXAA_QUALITY__SUBPIX_TRIM) * FXAA_QUALITY__SUBPIX_TRIM_SCALE;
blendL = min(FXAA_QUALITY__SUBPIX_CAP, blendL);

float edgeVert = abs(lumaNW + (-2.0 * lumaN) + lumaNE) + 2.0 * abs(lumaW  + (-2.0 * lumaM) + lumaE ) + abs(lumaSW + (-2.0 * lumaS) + lumaSE);
   float edgeHorz = abs(lumaNW + (-2.0 * lumaW) + lumaSW) + 2.0 * abs(lumaN  + (-2.0 * lumaM) + lumaS ) + abs(lumaNE + (-2.0 * lumaE) + lumaSE);
   bool horzSpan = edgeHorz >= edgeVert;

float lengthSign = horzSpan ? -rcpFrame.y : -rcpFrame.x;
if(!horzSpan) lumaN = lumaW;
if(!horzSpan) lumaS = lumaE;
float gradientN = abs(lumaN - lumaM);
float gradientS = abs(lumaS - lumaM);
lumaN = (lumaN + lumaM) * 0.5;
lumaS = (lumaS + lumaM) * 0.5;

bool pairN = gradientN >= gradientS;
if(!pairN) lumaN = lumaS;
if(!pairN) gradientN = gradientS;
if(!pairN) lengthSign *= -1.0;
float2 posN;
posN.x = pos.x + (horzSpan ? 0.0 : lengthSign * 0.5);
posN.y = pos.y + (horzSpan ? lengthSign * 0.5 : 0.0);


gradientN *= FXAA_SEARCH_THRESHOLD;

float2 posP = posN;
float2 offNP = horzSpan ?
float2(rcpFrame.x, 0.0) :
float2(0.0f, rcpFrame.y);
float lumaEndN;
float lumaEndP;
bool doneN = false;
bool doneP = false;
posN += offNP * (-1.5);
posP += offNP * ( 1.5);
for(int i = 0; i < FXAA_SEARCH_STEPS; i++)
{
lumaEndN = dot(FxaaTexTop(InputSampler, posN.xy).xyz, float3(0.299, 0.587, 0.114));
lumaEndP = dot(FxaaTexTop(InputSampler, posP.xy).xyz, float3(0.299, 0.587, 0.114));
bool doneN2 = abs(lumaEndN - lumaN) >= gradientN;
bool doneP2 = abs(lumaEndP - lumaN) >= gradientN;
if(doneN2 && !doneN) posN += offNP;
if(doneP2 && !doneP) posP -= offNP;
if(doneN2 && doneP2) break;
doneN = doneN2;
doneP = doneP2;
if(!doneN) posN -= offNP * 2.0;
if(!doneP) posP += offNP * 2.0;
}

float dstN = horzSpan ? pos.x - posN.x : pos.y - posN.y;
float dstP = horzSpan ? posP.x - pos.x : posP.y - pos.y;

bool directionN = dstN < dstP;
lumaEndN = directionN ? lumaEndN : lumaEndP;

if(((lumaM - lumaN) < 0.0) == ((lumaEndN - lumaN) < 0.0))
lengthSign = 0.0;

float spanLength = (dstP + dstN);
dstN = directionN ? dstN : dstP;
float subPixelOffset = 0.5 + (dstN * (-1.0/spanLength));
subPixelOffset += blendL * (1.0/8.0);
subPixelOffset *= lengthSign;
float3 rgbF = FxaaTexTop(InputSampler, float2(pos.x + (horzSpan ? 0.0 : subPixelOffset), pos.y + (horzSpan ? subPixelOffset : 0.0))).xyz;

  #if (FXAA_LINEAR == 1)
lumaL *= lumaL;
  #endif
  float lumaF = dot(rgbF, float3(0.299, 0.587, 0.114)) + (1.0/(65536.0*256.0));
  float lumaB = lerp(lumaF, lumaL, blendL);
  float scale = min(4.0, lumaB/lumaF);
  rgbF *= scale;

  return float4(rgbF, lumaM);
}

#ifdef NOISE
float4 PS_ProcessPass_ImageGrain(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
	float4 res;
	float2 coord = IN.txcoord.xy;
	res.xyz = tex2D(InputSampler, coord.xy).xyz;
	res.xyz += tex2D(SamplerNoise, coord.xy * 1024).xyz * Grain(float3(coord.xy, SEED));
	res.a = 1.0;
	return res;
}
#endif
//--------------------------------------------------------------------------------------
// Compiler
//--------------------------------------------------------------------------------------
technique PostProcess
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 main();
	}
#ifdef LIGHTSCATTERING
	pass P1
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessSunShafts();
		AlphaBlendEnable=True;
		SrcBlend = One;
		DestBlend = One;
	}
#endif
#ifdef BLOOM
	pass P2
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessBloom();
	}
#endif
}

technique PostProcess2
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#ifdef CHROMATICABBERATION
pass P1
{

VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 PS_PostProcessChroma();

DitherEnable=FALSE;
ZEnable=FALSE;
CullMode=NONE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
StencilEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#endif
}

technique PostProcess3
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
}

technique PostProcess4
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
}

technique PostProcess5
{
  pass P0
  {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 FxaaPixelShader();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#ifdef NOISE
pass P1
{

VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 PS_ProcessPass_ImageGrain();

DitherEnable=FALSE;
ZEnable=FALSE;
CullMode=NONE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
StencilEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
#endif
}
#ifdef ANAMORPHICFLARE
technique PostProcess6
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 CCShadow();
	}
}
#endif

#ifdef DOF
#ifdef ANAMORPHICFLARE
technique PostProcess7
#else
technique PostProcess6
#endif
{	
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_ProcessDoFBokeh();
		CullMode=NONE;
	}
	
}
#endif